package com.ruoyi.sqlbuilder.controller;

import java.util.List;

import com.ruoyi.common.utils.jdbc.JdbcUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sqlbuilder.domain.SbrDatabase;
import com.ruoyi.sqlbuilder.service.ISbrDatabaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据库信息Controller
 * 
 * @author yanzy
 * @date 2021-05-09
 */
@Controller
@RequestMapping("/sqlbuilder/sbrDatabase")
public class SbrDatabaseController extends BaseController
{
    private String prefix = "sqlbuilder/sbrDatabase";

    @Autowired
    private ISbrDatabaseService sbrDatabaseService;

    @RequiresPermissions("sqlbuilder:sbrDatabase:view")
    @GetMapping()
    public String sbrDatabase()
    {
        return prefix + "/sbrDatabase";
    }

    /**
     * 查询数据库信息列表
     */
    @RequiresPermissions("sqlbuilder:sbrDatabase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SbrDatabase sbrDatabase)
    {
        startPage();
        List<SbrDatabase> list = sbrDatabaseService.selectSbrDatabaseList(sbrDatabase);
        return getDataTable(list);
    }

    /**
     * 选择页
     * @param sbrDatabase
     * @return
     */
    @GetMapping(value = "/selectTable")
    public String selectTable(SbrDatabase sbrDatabase)
    {
        return prefix + "/select";
    }

    /**
     * 导出数据库信息列表
     */
    @RequiresPermissions("sqlbuilder:sbrDatabase:export")
    @Log(title = "数据库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SbrDatabase sbrDatabase)
    {
        List<SbrDatabase> list = sbrDatabaseService.selectSbrDatabaseList(sbrDatabase);
        ExcelUtil<SbrDatabase> util = new ExcelUtil<SbrDatabase>(SbrDatabase.class);
        return util.exportExcel(list, "数据库信息数据");
    }

    /**
     * 新增数据库信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据库信息
     */
    @RequiresPermissions("sqlbuilder:sbrDatabase:add")
    @Log(title = "数据库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SbrDatabase sbrDatabase)
    {
        return toAjax(sbrDatabaseService.insertSbrDatabase(sbrDatabase));
    }

    /**
     * 修改数据库信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SbrDatabase sbrDatabase = sbrDatabaseService.selectSbrDatabaseById(id);
        mmap.put("sbrDatabase", sbrDatabase);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据库信息
     */
    @RequiresPermissions("sqlbuilder:sbrDatabase:edit")
    @Log(title = "数据库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SbrDatabase sbrDatabase)
    {
        return toAjax(sbrDatabaseService.updateSbrDatabase(sbrDatabase));
    }

    /**
     * 删除数据库信息
     */
    @RequiresPermissions("sqlbuilder:sbrDatabase:remove")
    @Log(title = "数据库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sbrDatabaseService.deleteSbrDatabaseByIds(ids));
    }


    /**
     * 测试数据库连接
     * @param sbrDatabase
     * @return
     */
    @Log(title = "数据库信息", businessType = BusinessType.OTHER)
    @PostMapping("/testConn")
    @ResponseBody
    public AjaxResult testConn(SbrDatabase sbrDatabase) {
        String uri = sbrDatabase.getHost() + ":" + sbrDatabase.getPort();
        return toAjax(JdbcUtil.testConn(uri, sbrDatabase.getDatabaseName(), sbrDatabase.getUserName(), sbrDatabase.getPassword(), sbrDatabase.getDriverType()));
    }
}
